package com.link.play.fit.ai.bean

import android.text.TextUtils
import com.google.gson.Gson
import com.link.play.fit.ai.ext.d
import kotlinx.coroutines.Job
import java.io.Serializable


data class RecentlyEatenData(
    var name: String = "",
    var foodName: String? = null,
    var healthComment: String? = null,

    var showType: Int = 1, //数据状态 1正常  2加载中 3失败
    var type: Int = -1,//1 运动 2 扫描的食物 3自定义食物 4 接口搜索到的食物和 Ai 添加的食物
    var typeChild: Int = -1,//run weight des
    var exerciseCaloriesDaily: Int = 0,//运动增加的卡路里

    var caloriesDaily: Int = 0,//卡路里
    var carbsDaily: Int = 0,//碳水
    var proteinDaily: Int = 0,//蛋白质
    var fatsDaily: Int = 0,//脂肪
    var consumingTime: String = "",//消耗时长
    var createTime: String = "",//时间
    var intensity: String = "",//强度
    var img: String = "",//图片

    var timeDay: String = "", //日期

    var intensityPercent: Int = 0,//运动强度
    var time: Int = 0,//运动时间
    var describe: String = "",//运动描述
    var healthScore: Double? = null,

    var barcode: String = "",//二维码或者食品码
    var barcodeType: Int = 0,

    var metRate: Double = 0.0,
    var servings: Double = 1.0,//自定义食物用的分量
    var ingredients: String? = null, // 接口获取的食物
    var isBurnedSwitchV:Boolean = false,
    //首页数据列表更新用的
    var notifyType: Int = 0, //1 添加 , 2 是删除, 3是更新
//    var job: Job? = null
) : Serializable

fun RecentlyEatenData.toCollectFoodInfoData(): CollectFoodInfoData {
    val tmp = CollectFoodInfoData()
    if (TextUtils.isEmpty(foodName)) {
        tmp.name = ""
    } else {
        tmp.name = foodName.toString()
    }
    tmp.calories = caloriesDaily.toDouble()
    tmp.protein = proteinDaily.toDouble()
    tmp.carbs = carbsDaily.toDouble()
    tmp.fats = fatsDaily.toDouble()
    if (TextUtils.isEmpty(createTime)) {
        createTime = System.currentTimeMillis().toString()
    }
    tmp.timeMillisecond = createTime

    tmp.servings = servings
    tmp.img = img
    tmp.healthScore=healthScore
    return tmp
}

fun RecentlyEatenData.toFoodInfoBean() = FoodInfoBean().apply {
    "ingredients  ${this@toFoodInfoBean.ingredients}  ".d()
    val ing = Gson().fromJson(this@toFoodInfoBean.ingredients, InfoIngredientsBean::class.java)
    if (!TextUtils.isEmpty(this@toFoodInfoBean.createTime)) {
        //从收藏添加的食物,是新的时间戳,所以要把 InfoIngredientsBean 中的时间戳更新掉
        ing.timeMillisencond = this@toFoodInfoBean.createTime
    }
    ingredients = mutableListOf<InfoIngredientsBean>().apply {
        add(ing)
    }
}

//检查食物是否需要计算 true 需要计算,false 不需要计算
fun RecentlyEatenData.checkFoodInfoBean(): Boolean {
    runCatching {
        val ingredients = Gson().fromJson(ingredients, InfoIngredientsBean::class.java)
        val servingType = ingredients.servingTypes.find {
            if (it.id == ingredients.servingTypeId) {
                return@find true
            }
            return@find false
        }
        if (servingType == null) return true
        if ("g" == servingType.metricUnit || "G" == servingType.metricUnit) {
            return false
        }
    }
    return true
}